<?php
namespace Home\Controller;
use Think\Controller;
class TokenController extends Controller {
    public function index(){
		$User = M("Token"); // 实例化User对象
		$data = $User->where('id=1')->find();
		$date_c = time()-$data['date'];
		if($date_c>7000){
			$appid = 'wx41d436741216a814';
			$appsecret = '4748fdd3006011d61ebc71cc6061e522';
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
			$ch =curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$output = curl_exec($ch);
			curl_close($ch);
			$jsoninfo = json_decode($output, true);
			$access_token = $jsoninfo["access_token"];
			//$access_token = 'NCPbdgpMKfGbSrLAvmh3PFIrkscq7WPmmosQSV2ga9Oft2BxqJHbpfEuenv0bwWcW1wfoq6exOQZrbdFaiHKeBXn7eYx3dgohx2LH-HKJtepCJX3ycq37qU9eTrgHIgbPOJbAHACBW';
			$token['access_token'] = $access_token;
			$token['date'] = time();
			$User->where('id=1')->save($token); 
			return $access_token;
		}else{
			return $data['access_token'];
		};
    }
}